//
//  ViewController.swift
//  WanAndroidLearn
//
//  Created by TRS-Mobile on 2023/10/31.
//

import UIKit
import RxSwift
import RxCocoa
import SnapKit
import Then

class ViewController: BaseViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    override func makeUI() -> Void {
        super.makeUI()
        title = "功能测试区"
        testNetworking()
    }
    
    func testNetworking() -> Void {
        let topMargin = 20
        let inset = 20
        
        var items: [UIButton] = []
        
        let bt0 = createBt("文章列表").then {
            self.view.addSubview($0)
//            $0.snp.makeConstraints { make in
//                make.top.equalToSuperview().inset(topMargin)
//                make.left.equalToSuperview().inset(inset)
//            }
            items.append($0)
            $0.rx.tap
                .subscribe { [unowned self] _ in
                    self.navigationController?.pushViewController(ArticleListViewController(), animated: true)
                }
                .disposed(by: self.disposeBag)
        }
        
        _ = createBt("主题列表").then {
            self.view.addSubview($0)
//            $0.snp.makeConstraints { make in
//                make.top.equalToSuperview().inset(topMargin)
//                make.centerX.equalToSuperview()
//            }
            items.append($0)
            $0.rx.tap
                .subscribe { _ in
                    self.navigationController?.pushViewController(ThemeViewController(), animated: true)
                }
                .disposed(by: disposeBag)
        }
        _ = createBt("切换主题").then {
            self.view.addSubview($0)
//            $0.snp.makeConstraints { make in
//                make.top.equalToSuperview().inset(topMargin)
//                make.right.equalToSuperview().inset(20)
//            }
            items.append($0)
            $0.rx.tap
                .withLatestFrom(themeService.typeStream)
                .map { $0.toggled() }
                .bind(to: themeService.switcher)
                .disposed(by: disposeBag)
        }
        
        _ = createBt("学习StckView").then {
            view.addSubview($0)
//            $0.snp.makeConstraints { make in
//                make.top.equalTo(bt0.snp.bottom).offset(15)
//                make.left.equalTo(bt0)
//            }
            items.append($0)
            $0.rx.tap
                .subscribe { _ in
                    self.navigationController?.pushViewController(StackViewLearnViewController(), animated: true)
                }
                .disposed(by: disposeBag)
        }
        
        _ = createBt("跳转TabBar").then {
            view.addSubview($0)
//            $0.snp.makeConstraints { make in
//                make.top.equalTo(bt0.snp.bottom).offset(15)
//                make.centerX.equalToSuperview()
//            }
            items.append($0)
            $0.rx.tap
                .subscribe { _ in
                    let reactor = HomeTabBarReactor()
                    self.navigationController?.pushViewController(HomeTabBarController(reactor: reactor), animated: true)
                }
                .disposed(by: disposeBag)
        }
        _ = createBt("测试弹窗").then {
            view.addSubview($0)
//            $0.snp.makeConstraints { make in
//                make.top.equalTo(bt0.snp.bottom).offset(15)
//                make.centerX.equalToSuperview()
//            }
            items.append($0)
            $0.rx.tap
                .subscribe { [weak self] _ in
                    self?.showSheetTest()
                }
                .disposed(by: disposeBag)
        }
        var last = items.first!
        for (idx, item) in items.enumerated() {
            let isLeft = idx % 3 == 0
            let isRight = idx % 3 == 2
            item.snp.makeConstraints { make in
                if idx == 0 {
                    make.top.equalToSuperview().inset(20)
                } else if idx % 3 == 0 {
                    make.top.equalTo(last.snp.bottom).offset(20)
                } else {
                    make.top.equalTo(last)
                }
                if isLeft {
                    make.left.equalToSuperview().inset(20)
                } else if isRight {
                    make.right.equalToSuperview().inset(20)
                } else {
                    make.centerX.equalToSuperview()
                }
            }
            if isLeft {
                last = item
            }
        }
    }
    
    func createBt(_ title: String) -> UIButton {
        let bt = Button(type: .system)
        bt.setTitle(title, for: .normal)
        return bt
    }
    
    func showSheetTest() -> Void {
        let sheet = LoginSheetView(frame: CGRectMake(0, 0, UIScreen.main.bounds.width, 450))
        sheet.theme.backgroundColor = themed { $0.primary }
        sheet.isBlurEffectBG = false
        sheet.showInAnimationSheet()
    }
}

